package com.mystudyspring.studySpringBoot.controller;

import com.mystudyspring.studySpringBoot.domain.ApiResultDto;
import com.mystudyspring.studySpringBoot.dto.UserDto;
import com.mystudyspring.studySpringBoot.util.LoggerUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * home控制器
 *
 * @author lix
 * @create 2018-03-22 9:57
 */
@RestController
@RequestMapping(value = "/home")
@Api("swaggerDemoController相关的api")
public class HomeController {

    @ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get(HttpServletRequest request) {
        String str = "this is get action 1";
        request.setAttribute(LoggerUtils.LOGGER_RETURN, str);
        return str;
    }

    @ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
    @RequestMapping(value = "/list/{number}", method = RequestMethod.GET)
    public String list(@PathVariable int number) {
        System.out.println(20 / number);
        return "get list page successfully.";
    }

    @ApiOperation(value = "测试post json")
    @RequestMapping(value = "/testPost", method = RequestMethod.POST)
    public ApiResultDto testPost(@RequestBody(required = false) UserDto userDto) {
        ApiResultDto resp = ApiResultDto.newInstance();
        resp.setMsg("success");
        resp.setResult(userDto);
        return resp;
    }

    @ApiOperation(value = "测试post json")
    @RequestMapping(value = "/testPost2", method = RequestMethod.POST)
    public ApiResultDto testPost2(@RequestBody UserDto userDto) {
        ApiResultDto resp = ApiResultDto.newInstance();
        resp.setMsg("success");
        resp.setResult(userDto);
        return resp;
    }

    @ApiOperation(value = "测试get json")
    @RequestMapping(value = "/testGet2", method = RequestMethod.GET)
    public ApiResultDto testGet2(@RequestParam String name, Integer age) {
        ApiResultDto resp = ApiResultDto.newInstance();
        resp.setMsg("success");
        resp.setResult(new UserDto(name, age));
        return resp;
    }

    @ApiOperation(value = "测试get3 测试dto 实体传参数")
    @RequestMapping(value = "/testGet3", method = RequestMethod.GET)
    public ApiResultDto testGet3(@Valid UserDto userDto) {
        ApiResultDto resp = ApiResultDto.newInstance();
        resp.setMsg("success");
        resp.setResult(userDto);
        return resp;
    }

    @ApiOperation(value = "测试get4 测试dto 实体传参数,校验参数 ")
    @RequestMapping(value = "/testGet4", method = RequestMethod.GET)
    public ApiResultDto testGet4(@Valid UserDto userDto, BindingResult bindingResult) {
        ApiResultDto resp = ApiResultDto.newInstance();

        if(bindingResult.hasErrors()){
            StringBuffer sb = new StringBuffer();
            for(ObjectError objectError : bindingResult.getAllErrors()){
                sb.append(((FieldError)objectError).getField() +" : ").append(objectError.getDefaultMessage());
            }
            resp.setMsg(sb.toString());
            return resp;
        }
        resp.setMsg("success");
        resp.setResult(userDto);
        return resp;
    }
}
